﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FizzBuzz
{
    class LinqLocalQueriesDemo
    {
        // local queries in LINQ uses chain of decorator pattern
        // operates over IEnumerable
        public void Run()
        {
//            SimpleQueries();
//            SubQueries();
//            ObjectSelector();
        }

        // selects the result of a query into a new anonymous type using object initializer
        private void ObjectSelector()
        {

            string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };

            var temp =
              from n in names
              select new 
              {
                  Original = n,
                  Vowelless = n.Replace("a", "").Replace("e", "").Replace("i", "")
                               .Replace("o", "").Replace("u", "")
              };

            foreach(var t in temp)
            {
                Console.WriteLine("{0} - {1}", t.Original, t.Vowelless);
            }
        }


        public void SubQueries()
        {
            string [] names = {"A Tom", "B Dick", "C Harry"};
            IEnumerable<string> q = names.OrderBy(m => m.Split().Last());
//            Console.WriteLine("{0}", q);
            foreach(string name in q)
            {
                Console.WriteLine("{0}", name);
            }
        }

        public void SimpleQueries()
        {
            // lambda queries
            string[] names = {"Tom", "Dick", "Harry"};
            IEnumerable<string> e1 = System.Linq.Enumerable.Where(names, (string name) => name.Length >= 4);
            foreach (string n in e1)
            {
                Console.WriteLine("Name:{0}", n);
            }

            // using inference type
            var e2 = System.Linq.Enumerable.Where(names, (string name) => name.Length >= 4);
            foreach(string n in e2)
            {
                Console.WriteLine("Name:{0}", n);
            }

            // query comprehension syntax
            var e3 = from n in names
                     where n.Length >= 4
                     select n;

            foreach (string n in e3)
            {
                Console.WriteLine("Name:{0}", n);
            }
        }
    }
}
